package com.ruoyi.web.controller.topicComplain;

import java.util.List;

import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.topic.domain.Topic;
import com.ruoyi.topic.service.ITopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.topicComplain.domain.TopicComplain;
import com.ruoyi.topicComplain.service.ITopicComplainService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 话题举报 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-10-14
 */
@Controller
@RequestMapping("/topicComplain/topicComplain")
public class TopicComplainController extends BaseController
{
    private String prefix = "topicComplain/topicComplain";

	private String prefix2 = "topicComplain/topicComplainRecord";

	
	@Autowired
	private ITopicComplainService topicComplainService;

	@Autowired
	private ITopicService topicService;

	@Autowired
	private IUserService userService;
	
	@RequiresPermissions("topicComplain:topicComplain:view")
	@GetMapping()
	public String topicComplain()
	{
		return prefix + "/topic";
	}

	@RequiresPermissions("topicComplain:topicComplain:view")
	@GetMapping("/record")
	public String topicComplainRecord(ModelMap mmap)
	{
		Topic topic = new Topic();
		List<Topic> topicList = topicService.selectTopicList(topic);
		mmap.put("topicList", topicList);
		return prefix2 + "/topicComplain";
	}
	
	/**
	 * 查询话题举报列表
	 */
	@RequiresPermissions("topicComplain:topicComplain:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TopicComplain topicComplain)
	{
		startPage();
		topicComplain.setComplainVerify(0);
        List<TopicComplain> list = topicComplainService.selectTopicComplainList(topicComplain);
		return getDataTable(list);
	}



	/**
	 * 查询话题举报列表
	 */
	@RequiresPermissions("topicComplain:topicComplain:list")
	@PostMapping("/recordList")
	@ResponseBody
	public TableDataInfo recordList(TopicComplain topicComplain)
	{
		startPage();
		topicComplain.setValid(2);
		List<TopicComplain> list = topicComplainService.selectTopicComplainList(topicComplain);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出话题举报列表
	 */
	@RequiresPermissions("topicComplain:topicComplain:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopicComplain topicComplain)
    {
    	List<TopicComplain> list = topicComplainService.selectTopicComplainList(topicComplain);
        ExcelUtil<TopicComplain> util = new ExcelUtil<TopicComplain>(TopicComplain.class);
        return util.exportExcel(list, "topicComplain");
    }
	
	/**
	 * 新增话题举报
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存话题举报
	 */
	@RequiresPermissions("topicComplain:topicComplain:add")
	@Log(title = "话题举报", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TopicComplain topicComplain)
	{		
		return toAjax(topicComplainService.insertTopicComplain(topicComplain));
	}

	/**
	 * 修改话题举报
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		String topicName = "";
		String userName = "";
		TopicComplain topicComplain = topicComplainService.selectTopicComplainById(id);
		Integer userId = topicComplain.getComplainUserId();
		Integer topicId = topicComplain.getTopicId();
		Topic topic = topicService.selectTopicById(topicId);
		if (topic != null) {
			topicName = topic.getTopicName();
		}
		User user = userService.selectUserById(userId);
		if (user != null) {
			userName = user.getNickname();
		}
		mmap.put("userName", userName);
		mmap.put("topicName", topicName);
		mmap.put("topicComplain", topicComplain);
		return prefix + "/edit";
	}
	
	/**
	 * 修改保存话题举报
	 */
	@RequiresPermissions("topicComplain:topicComplain:edit")
	@Log(title = "话题举报", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TopicComplain topicComplain)
	{		
		return toAjax(topicComplainService.updateTopicComplain(topicComplain));
	}
	
	/**
	 * 删除话题举报
	 */
	@RequiresPermissions("topicComplain:topicComplain:remove")
	@Log(title = "话题举报", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(topicComplainService.deleteTopicComplainByIds(ids));
	}

	@PostMapping("/ignoreVerify")
	@ResponseBody
	public AjaxResult ignoreVerify(Integer id) throws Exception {
		TopicComplain topicComplain = topicComplainService.selectTopicComplainById(id);
		topicComplain.setComplainVerify(1);
//		Integer topicId = topicComplain.getTopicId();
//		Topic persist = topicService.selectTopicById(topicId);
//		topicService.updateTopic(persist);
		return toAjax(topicComplainService.updateTopicComplain(topicComplain));
	}

	@PostMapping("/hideVerify")
	@ResponseBody
	public AjaxResult hideVerify(Integer id) throws Exception {
		TopicComplain topicComplain = topicComplainService.selectTopicComplainById(id);
		topicComplain.setComplainVerify(2);
		topicComplainService.updateTopicComplain(topicComplain);
		Integer topicId = topicComplain.getTopicId();
		Topic persist = topicService.selectTopicById(topicId);
		persist.setVerifyState("2");
		return toAjax(topicService.updateTopic(persist));
	}


}
